package yu.zhu.todo.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import yu.zhu.todo.entity.PageResult;
import yu.zhu.todo.entity.StatusCode;
import yu.zhu.todo.entity.Result;
import yu.zhu.todo.pojo.TodoDto;
import yu.zhu.todo.service.TodoService;

import java.util.Map;


/**
 * @Author yuzhu
 * @Date 2020/11/10 22:52
 */
@RestController
@Api(tags = "Todo相关接口")
@RequestMapping("todo")
@CrossOrigin
public class TodoController {

    @Value("${data.type}")
    private String dataBeanName;

    private TodoService todoService;

    @Autowired
    public void setTodoService(ApplicationContext context){
        todoService = context.getBean(dataBeanName,TodoService.class);
    }

    @ApiOperation("保存todo")
    @PostMapping
    public Result saveTodo(@RequestBody TodoDto todo){
        return  new Result(true, StatusCode.OK, "保存成功",todoService.saveTodo(todo));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除todo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "todo的id")
    })
    public  Result deleteTodo(@PathVariable String id){
        todoService.deleteTodo(id);
        return  new Result(true, StatusCode.OK, "删除成功");
    }

    @PutMapping("{id}")
    @ApiOperation("根据id修改todo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "todo的id")
    })
    public Result updateTodo(@PathVariable String id,@RequestBody TodoDto todo){
        todo.setId(id);
        return  new Result(true, StatusCode.OK, "修改成功", todoService.updateTodo(todo));
    }


    @GetMapping
    @ApiOperation("查询所有todo")
    public Result findAllTodo(){
        return  new Result(true, StatusCode.OK, "查询成功", todoService.findAllTodo());
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询todo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "todo的id")
    })
    public Result findByTodoId(@PathVariable String id){
        return  new Result(true, StatusCode.OK, "查询成功", todoService.findByTodoId(id));
    }


}
